<template>
  <div id="product-detail">
    <nav class="top-navbar" :class="{ fixed: isSticky }">
      <div class="back">
        <button>&lt;</button>
      </div>
      <div class="nav-right">
        <a href="#" class="nav-link">首页</a>
        <a href="#" class="nav-link">个人中心</a>
        <a href="#" class="nav-link">购物车</a>
        <a href="#" class="nav-link">收藏夹</a>
        <a href="#" class="nav-link">帮助中心</a>
        <button class="sign-in-button" @click="showLoginDialog = true">
          Sign in
        </button>
        <button class="register-button" @click="showRegisterDialog = true">
          Register
        </button>
        <button v-if="isLoggedIn" class="sign-out-button" @click="logout">
          Sign out
        </button>
      </div>
    </nav>
    <div class="product-detail-container">
      <h2>{{ product.name }}</h2>
      <img :src="product.image" alt="product" class="product-detail-image" />
      <p class="product-description">{{ product.description }}</p>
      <span class="product-price">¥{{ product.price }}</span>
      <button class="add-to-cart-button">加入购物车</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import { useRoute, useRouter } from "vue-router";

const route = useRoute();
const router = useRouter();

const product = ref({
  id: null,
  name: "",
  price: 0,
  image: "",
  description: "",
});

const isSticky = ref(false);

const mockProducts = {
  1: {
    name: "商品1",
    price: 100,
    image: "https://via.placeholder.com/300",
    description: "这是商品1的详细描述。",
  },
  2: {
    name: "商品2",
    price: 200,
    image: "https://via.placeholder.com/300",
    description: "这是商品2的详细描述。",
  },
  3: {
    name: "商品3",
    price: 150,
    image: "https://via.placeholder.com/300",
    description: "这是商品3的详细描述。",
  },
};

const handleScroll = () => {
  isSticky.value = window.pageYOffset > 0;
};

const goBack = () => {
  router.push({ name: "ProductHome" });
};

onMounted(() => {
  const productId = parseInt(route.params.id, 10);
  product.value = mockProducts[productId] || {
    name: "未知商品",
    price: 0,
    image: "",
    description: "",
  };
  window.addEventListener("scroll", handleScroll);
});

onBeforeUnmount(() => {
  window.removeEventListener("scroll", handleScroll);
});
</script>

<style scoped>
.product-detail-container {
  padding: 20px;
  text-align: center;
}
.product-detail-image {
  width: 300px;
  height: auto;
  margin: 20px 0;
}
.product-description {
  margin: 20px 0;
  font-size: 16px;
  color: #666;
}
.add-to-cart-button {
  padding: 10px 20px;
  background-color: #333;
  color: white;
  border: none;
  cursor: pointer;
}
* {
  margin: 0 auto;
  padding: 0;
}
#app {
  width: 100vw;
  height: auto;
  margin: 0;
  padding-top: 0;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  /* -webkit-font-smoothing: antialiased; */
  /* -moz-osx-font-smoothing: grayscale; */
  text-align: center;
  color: #2c3e50;
}
.top-navbar {
  display: flex;
  justify-content: space-between;
  gap: 20vh;
  background-color: #f4f4f4;
  padding: 10px;
  position: relative;
  z-index: 100px;
  transition: all 0.3s ease;
}
.back {
  display: flex;
  justify-content: start;
}
.nav-right {
  display: flex;
  align-content: flex-end;
}
.nav-link:hover {
  color: white;
  background-color: #333;
  border-radius: 20%;
}
.fixed {
  position: sticky;
  top: 0;
  width: 100%;
  background-color: #f4f4f4;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.nav-link {
  margin-right: 20px;
  text-decoration: none;
  color: #333;
}
.sign-in-button,
.register-button,
.sign-out-button {
  background-color: #333;
  color: white;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
}
</style>
